package leetcodecompetition.D368;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Siantar
 * @Date: 2023-10-22-10:58
 * @Description: 1.0
 */
public class T100097 {
    public int minGroupsForValidAssignment(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        List<Integer> list = new ArrayList<>();
        list.addAll(map.values());
        System.out.println(list);
        int min = list.stream().min((a, b) -> a - b).get();
        int res = Integer.MAX_VALUE;
        for (int i = 1; i <= min; i++) {
            int sum = 0;
            for (int p : list) {
                int r = get(i, p);
                if (r == -1) {
                    sum = -1;
                    break;
                }
                sum += r;
            }
            System.out.println(i+" "+sum);
            res = Math.min(res, sum == -1 ? Integer.MAX_VALUE : sum);
        }
        return res;
    }

    public int get(int d, int p) {
        int t = p / d, l = p % d, r = t;
        if (t < l) return -1;
        t -= l;
        while (t > d) {
            t -= d+1;
            r--;
        }
        System.out.println("  "+r);
        return r;
    }
}
